//
//  WBNavigationController.swift
//  仿新浪微博
//
//  Created by 罗超 on 2017/5/3.
//  Copyright © 2017年 LuoChao. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认 NavigationBar
        navigationBar.isHidden = true
    }
    
    //重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //根控制器不需要隐藏
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                
                //判断控制器的级数,第一级显示上级的标题,其他显示返回
                let title = childViewControllers.count == 1 ? (childViewControllers.first?.title ?? "返回") : "返回"
                
                //取出自定义的 navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
                
            }
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    // pop 返回上一级控制器
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }

}
